package com.anxin.act.task.vo;

import com.anxin.act.task.entity.ViewHiTask;
import com.anxin.act.task.entity.WfActHiComment;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.DateUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.file.entity.FileBase;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.apache.commons.collections.map.HashedMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 已办任务
 * @author: liuxiangyu
 * @date: 2020/5/6 16:59
 */
public class HiTaskVo {

    /**
     * ID
     */
    private String id;

    /**
     * Activiti任务的ID
     */
    private String taskId;

    /**
     * 执行环节
     */
    private String taskName;

    /**
     * 执行人
     */
    private List<String> approver;
    /**
     * 执行人信息
     */
    private List<SysUser> approverUserList;

    /**
     * 审批人Logo
     */
    private String approverLogo;

    /**
     * 抄送用户
     */
    private String ccUser;

    /**
     * 开始时间
     */
    private Date startTime;

    /**
     * 审批时间
     */
    private Date endTime;

    /**
     * 审批意见
     */
    private List<WfActHiComment> commentList;

    /**
     * 附件集合
     */
    private List<FileBase> taskAttachments;

    /**
     * 任务提交类型
     */
    private String submitType;

    /**
     * 节点跳转操作用户
     */
    private String adjustNodeUser;

    /**
     * 跳转节点
     */
    private String adjustNode;

    /**
     * 启动用户
     */
    private String startUser;

    /**
     * 启动用户
     */
    private String startDate;

    /**
     * 终止用户
     */
    private String stopUser;

    /**
     * 终止时间
     */
    private String stopDate;

    /**
     * 终止原因
     */
    private String stopReason;


    /**
     * 当前审批用户 已读未读标识存储。
     * 因为存在会签，需要用map存储
     */
    private Map<String, Boolean> readStatusMap = new HashedMap();


    public HiTaskVo(){

    }

    /**
     * 获取流程启动信息
     * @param processVar
     */
    public HiTaskVo(Map<String, Object> processVar){
       this.taskName = processVar.get("applyNickName").toString() + " 于 " + DateUtils.format(new Date(Long.parseLong(processVar.get("applyTime").toString()))) + "启动流程";
    }

    /**
     * 获取流程终止信息
     * @param actId
     */
    public HiTaskVo(String actId,Map<String, Object> processVar){
        HistoricProcessInstance hiProcessInstance = ActProcessUtils.getHiProcessInstance(actId);
        // 查看流程是否已经终止
        if (StringUtils.isNotBlank(hiProcessInstance.getDeleteReason())){
            String actDeleteUser = processVar.get("actDeleteUser").toString();
            if (StringUtils.isNotBlank(actDeleteUser)){
                SysUser stopUser = UserUtils.getUserByUserName(actDeleteUser);
                if (stopUser !=null ){
                    actDeleteUser = stopUser.getNickName();
                }
            }

            StringBuffer stopInfo = new StringBuffer();
            stopInfo.append(actDeleteUser + " 于 " + DateUtils.dateTime(hiProcessInstance.getEndTime())  + "c");
            stopInfo.append("  <br/>终止原因:" + hiProcessInstance.getDeleteReason());
            this.taskName = stopInfo.toString();

        }

    }

    /**
     * 根据Activiti历史任务初始化
     * @param historicTaskInstance
     */
    public HiTaskVo(HistoricTaskInstance historicTaskInstance){
        this.taskId = historicTaskInstance.getId();
        this.taskName = this.getTaskName(historicTaskInstance);
        this.approver = UserUtils.userNameToNickName(ActUtils.getAssigneeByTask(historicTaskInstance));
        this.approverLogo = UserUtils.getAvatarByUserName(historicTaskInstance.getAssignee());
        this.startTime = historicTaskInstance.getStartTime();
        this.endTime = historicTaskInstance.getEndTime();
    }

    /**
     * 根据历史任务初始化
     * @param viewHiTask
     */
    public HiTaskVo(ViewHiTask viewHiTask){
        this.id = viewHiTask.getId();
        this.taskId = viewHiTask.getTaskId();
        this.taskName = viewHiTask.getTaskName();
        this.approver = UserUtils.userNameToNickName(ActUtils.getAssigneeByTask(viewHiTask));
        this.approverLogo = viewHiTask.getAssigneeAvatar();
        this.startTime = viewHiTask.getStartTime();
        this.endTime = viewHiTask.getApprovalTime();

    }


    /**
     * 根据历史任务初始化
     * @param viewHiTask
     */
    public HiTaskVo(ViewHiTask viewHiTask,Map<String, String> wfTaskReadMap){
        this.id = viewHiTask.getId();
        this.taskId = viewHiTask.getTaskId();
        this.taskName = viewHiTask.getTaskName();
        this.approverLogo = viewHiTask.getAssigneeAvatar();
        this.startTime = viewHiTask.getStartTime();
        this.endTime = viewHiTask.getApprovalTime();
        this.approverUserList = ActUtils.getAssigneeByTask(viewHiTask).stream().map(x -> UserUtils.getUserByUserName(x)).collect(Collectors.toList());

//        if (endTime != null){
//            List<String> assigneeUserNameList = ActUtils.getAssigneeByTask(viewHiTask);
//            this.approver = this.getAssigneeRead(assigneeUserNameList,wfTaskReadMap,viewHiTask.getTaskId());
//        }else {
//            this.approver = UserUtils.userNameToNickName(ActUtils.getAssigneeByTask(viewHiTask));
//        }
        this.approver = UserUtils.userNameToNickName(ActUtils.getAssigneeByTask(viewHiTask));

    }

    public List<String> getAssigneeRead(List<String> assigneeUserNameList,Map<String, String> wfTaskReadMap,String taskId){
        List<String> res = new ArrayList<>();
        for (int i = 0; i < assigneeUserNameList.size(); i++) {
            SysUser sysUser = UserUtils.getUserByUserName(assigneeUserNameList.get(i));
            if (sysUser != null) {
                String read = wfTaskReadMap.get(taskId + "_" + sysUser.getId());
//                res.add(sysUser.getNickName() + "(" +  +")");
            }
        }
        return res;
    }


    /**
     * 获取历史任务名称-给任务增加 (前加签) or (后加签等)
     * @param historicTaskInstance
     * @return
     */
    public String getTaskName(HistoricTaskInstance historicTaskInstance){
        if (historicTaskInstance.getTaskDefinitionKey().endsWith("actBefore") && !historicTaskInstance.getName().endsWith("(前加签)")){
            return historicTaskInstance.getName() + "(前加签)";
        }if (historicTaskInstance.getTaskDefinitionKey().endsWith("actAfter") && !historicTaskInstance.getName().endsWith("(后加签)")){
            return historicTaskInstance.getName() + "(后加签)";
        }if (historicTaskInstance.getTaskDefinitionKey().endsWith("actEntrustment") && !historicTaskInstance.getName().endsWith("(转签)")){
            return historicTaskInstance.getName() + "(转签)";
        }if (historicTaskInstance.getTaskDefinitionKey().endsWith("reading") && !historicTaskInstance.getName().endsWith("(阅办人)")){
            return historicTaskInstance.getName() + "(阅办人)";
        }
        return historicTaskInstance.getName();
    }


    public String getAdjustNodeUser() {
        return adjustNodeUser;
    }

    public void setAdjustNodeUser(String adjustNodeUser) {
        this.adjustNodeUser = adjustNodeUser;
    }

    public String getAdjustNode() {
        return adjustNode;
    }

    public void setAdjustNode(String adjustNode) {
        this.adjustNode = adjustNode;
    }

    public List<FileBase> getTaskAttachments() {
        return taskAttachments;
    }

    public void setTaskAttachments(List<FileBase> taskAttachments) {
        this.taskAttachments = taskAttachments;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public List<String> getApprover() {
        return approver;
    }

    public void setApprover(List<String> approver) {
        this.approver = approver;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public List<WfActHiComment> getCommentList() {
        return commentList;
    }

    public void setCommentList(List<WfActHiComment> commentList) {
        this.commentList = commentList;
    }

    public String getApproverLogo() {
        return approverLogo;
    }

    public void setApproverLogo(String approverLogo) {
        this.approverLogo = approverLogo;
    }

    public String getSubmitType() {
        return submitType;
    }

    public void setSubmitType(String submitType) {
        this.submitType = submitType;
    }

    public String getStartUser() {
        return startUser;
    }

    public void setStartUser(String startUser) {
        this.startUser = startUser;
    }

    public String getStopUser() {
        return stopUser;
    }

    public void setStopUser(String stopUser) {
        this.stopUser = stopUser;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getStopDate() {
        return stopDate;
    }

    public void setStopDate(String stopDate) {
        this.stopDate = stopDate;
    }

    public String getStopReason() {
        return stopReason;
    }

    public void setStopReason(String stopReason) {
        this.stopReason = stopReason;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getCcUser() {
        return ccUser;
    }

    public void setCcUser(String ccUser) {
        this.ccUser = ccUser;
    }

    public Map<String, Boolean> getReadStatusMap() {
        return readStatusMap;
    }

    public void setReadStatusMap(Map<String, Boolean> readStatusMap) {
        this.readStatusMap = readStatusMap;
    }

    public List<SysUser> getApproverUserList() {
        return approverUserList;
    }

    public void setApproverUserList(List<SysUser> approverUserList) {
        this.approverUserList = approverUserList;
    }
}
